const app = getApp();
const util = require('../../../utils/util.js');
const lbbService = require('../../../pages/service/service.js');
const lbbTaskService = require('../service/task-service.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userList:[],
        cityShow: '0',
        userName: '全部师傅',
        cityName:'全部城市',
        isShowCity:false,
        userId:-1,
        selectMonth:(new Date().getYear()+1900) + '-' +(new Date().getMonth()+1),
        user_animationData: {},
        city_animationData: {}, 
        month_animationData: {}, 
        markShow:false,

        minDate:new Date(2010, 0, 1).getTime(),
        currentDate:new Date().getTime(),
        searchText: '',
        pageNo:1,
        pageSize:10,
    },
    //点击选择城市
    onCity(e) {
        console.log(e)
        let selectCityId = e.currentTarget.dataset.cityid
        this.toggleAnimation(0)
        this.setData({
            cityId: e.currentTarget.dataset.cityid,
            isShowCity: false,
            cityName:e.currentTarget.dataset.name,
            cityShow: '0',
            city_animationData: this.animation.export(),
            wordValue:'',
            cityList:this.data.allcityList
        })
    },
    //关键词搜索城市
    onkeyword(e) {
        let cityList=this.data.allcityList.filter(item=>{
            if(item.label.indexOf(e.detail)!=-1){
                return item
            }
        })
        this.setData({
            cityList:cityList,
            wordValue:e.detail
        })
    },
    //获取城市列表
    getCityList() {
        let that = this
        lbbTaskService.getFollowCityList({
            level: 1
        }, function (res) {
            let cityName = res.data.find(item => item.value == that.data.value)?.label
            that.setData({
                cityList: res.data,
                allcityList:JSON.parse(JSON.stringify(res.data)),
                cityName: cityName
            })
        })
    },
    //上拉加载更多
    eventhandle(e) {
        let that = this
        let pagenum = Number(that.data.pageNo)
        if (pagenum >= that.data.TotalPage) {
            that.setData({
                loading: false,
                loaded: true
            })
        } else {
            pagenum = pagenum + 1
            that.data.pageNo = pagenum.toString()
            that.setData({
                loading: true,
                loaded: false,
                pageNo:that.data.pageNo
            })
            that.getHospitalList()
        }
    },
    inputName: function (e) {
        clearTimeout(this.data.timer)
        // let inputName = e.detail.value
        this.data.timer = setTimeout(() => {
          console.log(e.detail.value)
          this.setData({
            inputName: e.detail.value
          })
        }, 500);
        // this.setData({
        //   timer: timer
        // })
    },
    //搜索的关键字
    onChangeValue(e) {
        this.setData({
            searchText: e.detail
        })
    },
    //关键字搜索
    onSearch() {
        let that = this
        that.getHospitalList()
    },
    query(list) {
        let that = this
        return list.filter(item => item.hospitalName.indexOf(that.data.searchText ?? '') != -1)
    },
    //创建动画
    toggleAnimation: function(angle) {
        var animation = wx.createAnimation({
          timingFunction: "ease"
        })
        this.animation = animation;
        animation.rotate(angle).step()
    },
    userToggle: function() {
        this.pickUp()
        if (this.data.cityShow=='1') {
          this.setData({
            cityShow: '0',
            isShowCity:false,
          })
        } else {
          this.toggleAnimation(180)
          this.setData({
            cityShow: '1',
            isShowCity:false,
          })
        }
        this.setData({
          user_animationData: this.animation.export()
        })
    },
    cityToggle: function() {
        this.pickUp()
        if (this.data.cityShow=='2') {
          this.setData({
            cityShow: '0',
            isShowCity:false,
          })
        } else {
          this.toggleAnimation(180)
          this.setData({
            cityShow: '2',
            isShowCity:true,
          })
        }
        this.setData({
            city_animationData: this.animation.export()
        })
    },
    monthToggle: function() {
        this.pickUp()
        if (this.data.cityShow=='3') {
          this.setData({
            cityShow: '0',
            isShowCity:false,
          })
        } else {
          this.toggleAnimation(180)
          this.setData({
            cityShow: '0',
            isShowCity:false,
            markShow:true
          })
        }
        this.setData({
          month_animationData: this.animation.export()
        })
    },
    formatDate(date) {
        date = new Date(date);
        return `${date.getYear() +1900 }-${date.getMonth() + 1}`;
    },
    onMarkClose() {
        this.toggleAnimation(0)
        this.setData({ markShow: false,month_animationData: this.animation.export() });
    },
    onMarkConfirm(event) {
        this.data.selectMonth = this.formatDate(event.detail)
        this.toggleAnimation(0)
        this.setData({
          markShow: false,
          selectMonth: this.data.selectMonth,
          cityShow: '0',
          month_animationData: this.animation.export()
        });
    },
    setUser:function (e) {
        console.log("setUser-->",e)
       
        let userIndex = e.target.dataset.index; //当前点击的索引
        let userName = e.target.dataset.name;
        let userId = e.target.dataset.id;
        this.toggleAnimation(0)
        this.setData({
            cityShow: '0',
            userIndex,
            userId,
            userName,
            user_animationData: this.animation.export()
        })
    },
    setCity:function (e) {
        console.log("setCity-->",e)
       
        let cityIndex = e.target.dataset.index; //当前点击的索引
        let cityName = e.target.dataset.cityName;
        let cityId = e.target.dataset.cityId;
        this.toggleAnimation(0)
        this.setData({
            cityShow: '0',
            cityIndex,
            cityId,
            cityName,
            city_animationData: this.animation.export()
        })
    },
    cancelSelect: function(){
        this.pickUp()
        this.setData({
          cityShow: '0'
        })
    },
    pickUp: function(){
        if (this.data.cityShow == '1') {
            this.toggleAnimation(0)
            this.setData({
              user_animationData: this.animation.export()
            })
        } else if (this.data.cityShow == '2') {
          this.toggleAnimation(0)
          this.setData({
            city_animationData: this.animation.export()
          })
        } else if (this.data.cityShow == '3') {
          this.toggleAnimation(0)
          this.setData({
            month_animationData: this.animation.export()
          })
        }
    },
    getUserList() {
        let that = this
        lbbTaskService.getDeskUserList(null,function(res){
            if (res.data.length == 1) {
                that.setData({
                    userId: res.data[0].id,
                    userName: res.data[0].name
                })
                that.getHospitalList()
            } else {
                wx.getUserInfo({
                    success(res) {
                        that.setData({
                            userName: res.userInfo.nickName,
                        })
                    }
                })
            }
            that.setData({
                userList:res.data
            })
        })
    },
    nagativeToNext(e) {
        console.log(e)
        wx.navigateTo({
          url: '/pkgCost/pages/hospital-follow-up-detail/index?hospitalInfo='+JSON.stringify(e.currentTarget.dataset.item)+'&userId='+this.data.userId,
        })
    },
    // 跟进记录列表
    getHospitalList(){
        let that = this
        let parmas = {
            pageNo:that.data.pageNo,
            pageSize:that.data.pageSize,
            userId:that.data.userId,
            month:that.data.selectMonth
        }
        if(that.data.searchText){
            parmas['mechanismName'] = that.data.mechanismName
        }
        if(that.data.cityId) {
            parmas['cityId'] = that.data.cityId
        }
        wx.showLoading({
          title: '加载中',
        })
        lbbTaskService.mechanismFollowRecord(parmas,function(res){
            console.log(res.data)
            if(that.data.pageNo==1) {
                let lists = [ ...res.data.records]
                if(lists.length==0) {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        hospitalList:lists
                    })
                    wx.hideLoading()
                } else {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        hospitalList:lists
                    })
                    wx.hideLoading()
                }
            } else {
                let lists = [...that.data.hospitalList, ...res.data.records]
                console.log(lists)
                if(lists.length==0) {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        hospitalList:lists
                    })
                    wx.hideLoading()
                } else {
                    that.setData({
                        loading: false,
                        loaded: true,
                        TotalPage:res.data.pages,
                        hospitalList:lists
                    })
                    wx.hideLoading()
                }
            }
            console.log(that.data.hospitalList)
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getUserList()
        this.getCityList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})